import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        menuList: []

    },
    mutations: {
        // 保存菜单数据
        saveMenuList(state, menuList) {
            state.menuList = menuList
        }
    },
    actions: {
        // 调用接口，获得菜单数据
        getMenuList({ commit }, params = {}) {
            return new Promise((resolve, reject) => {
                axios.get("/api/menulist", { params: params })
                    .then((result) => {
                        commit('saveMenuList', result.data.list)
                        resolve(result)
                    })
                    .catch((err) => {
                        console.log(err);
                        reject(err)
                    });
            })
        },
    }
})

export default store